//
//  LoginViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/29.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "LoginViewController.h"
#import "FoundPasswordViewController.h"
#import "RegisterViewController.h"
#import "MainViewController.h"
#import "DataProvider.h"
#import "SVProgressHUD.h"
#import "CommonDef.h"

@interface LoginViewController () <UITextFieldDelegate>
//账号
@property (nonatomic, strong) UITextField * text_account;
//密码
@property (nonatomic, strong) UITextField * text_password;

@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navigationController];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    self.navigationItem.title = @"登录";
    
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}
//点击事件(右)
- (void)rightBarButtonItemAction:(UIBarButtonItem *)bar
{
    //注册账号
    
//    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请到电脑端注册账号" preferredStyle:(UIAlertControllerStyleAlert)];
//    
//    [self presentViewController:alert animated:YES completion:^{
//        
//    }];
//    
//    UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//        
//    }];
//    
//    [alert addAction:action];
    
    
    RegisterViewController * registerViewController = [[RegisterViewController alloc] init];
    [self showViewController:registerViewController sender:nil];
}
#pragma mark - 布局
- (void)p_setupView
{
    UIView * view_account = [[UIView alloc] initWithFrame:CGRectMake(10, 64 + 10, self.view.frame.size.width - 20, 50)];
    view_account.backgroundColor = [UIColor whiteColor];
    view_account.layer.cornerRadius = 5;
    view_account.layer.borderColor = [UIColor grayColor].CGColor;
    view_account.layer.borderWidth = 1;
    [self.view addSubview:view_account];
    
    UILabel * label_account = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 60, 40)];
    label_account.text = @"账号";
    label_account.font = [UIFont systemFontOfSize:17];
    [view_account addSubview:label_account];
    
    self.text_account = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_account.frame) + 0, 5, 200, 40)];
//    self.text_account.backgroundColor = [UIColor orangeColor];
    self.text_account.placeholder = @"请输入手机号";
    [view_account addSubview:self.text_account];
    self.text_account.delegate = self;
    
    
    UIView * view_password = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(view_account.frame ) + 10, self.view.frame.size.width - 20, 50)];
    view_password.backgroundColor = [UIColor whiteColor];
    view_password.layer.cornerRadius = 5;
    view_password.layer.borderColor = [UIColor grayColor].CGColor;
    view_password.layer.borderWidth = 1;
    [self.view addSubview:view_password];
    
    UILabel * label_password = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 60, 40)];
    label_password.text = @"密码";
    label_password.font = [UIFont systemFontOfSize:17];
    [view_password addSubview:label_password];
    
    self.text_password = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_password.frame) + 0, 5, 200, 40)];
    self.text_password.placeholder = @"请输入密码";
    self.text_password.secureTextEntry = YES;
    [view_password addSubview:self.text_password];
    self.text_password.delegate = self;
    
    //登陆
    UIButton * btn_login = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_login.frame = CGRectMake(20, CGRectGetMaxY(view_password.frame) + 20, self.view.frame.size.width - 40 , 40);
    [btn_login setTitle:@"立即登录" forState:(UIControlStateNormal)];
    [btn_login setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_login.layer.cornerRadius = 4;
    btn_login.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn_login];
    [btn_login addTarget:self action:@selector(btn_loginAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //找回密码
    UIButton * btn_foundPassword = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_foundPassword.frame = CGRectMake(self.view.frame.size.width - 90, CGRectGetMaxY(btn_login.frame) + 5, 80, 30);
//    btn_foundPassword.backgroundColor = [UIColor orangeColor];
    [btn_foundPassword setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [btn_foundPassword  setTitle:@"找回密码" forState:(UIControlStateNormal)];
    btn_foundPassword.titleLabel.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:btn_foundPassword];
    [btn_foundPassword addTarget:self action:@selector(btn_foundPasswordAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 登录点击事件
- (void)btn_loginAction:(UIButton *)sender
{
    
//    if(self.text_account.text.length != 11){
//        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请正确填写手机号" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
//        [alert show];
//        return;
//    }
    if(self.text_password.text.length == 0){
        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    DataProvider * dataprovider=[[DataProvider alloc] init];
    [dataprovider setDelegateObject:self setBackFunctionName:@"Login_inCall:"];
    [dataprovider Login_inwith:self.text_account.text andpwd:self.text_password.text];
}

- (void)Login_inCall:(id )dict
{
//    NSLog(@"%@",dict);
    
    if ([dict[@"status"][@"succeed"] intValue]==1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRootView1" object:nil];
        @try {
            set_sp(@"memberid",dict[@"data"][@"memberid"]);
            set_sp(@"account",self.text_account.text);
            set_sp(@"password",self.text_password.text);
            if ([dict[@"status"][@"errcode"] intValue]==1111||[dict[@"status"][@"errcode"] intValue]==3333) {
//                [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"Login_showAlert" object:dict[@"status"][@"errdesc"]];
                set_sp(@"errcode", @"1111");
                set_sp(@"errdesc", dict[@"status"][@"errdesc"]);
                return;
            }
            else if ([dict[@"status"][@"errcode"] intValue]==2222)
            {
//                [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
                set_sp(@"errcode", @"2222");
                set_sp(@"errdesc", dict[@"status"][@"errdesc"]);
                return;
            }
            set_sp(@"errcode", @"0000");
            set_sp(@"errdesc", dict[@"status"][@"errdesc"]);
            
            NSUserDefaults * userdefault=[NSUserDefaults standardUserDefaults];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"categoryid"]] forKey:@"categoryid"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"merchantid"]] forKey:@"merchantid"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"memberid"]] forKey:@"memberid"];

            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"address"]] forKey:@"address"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"announcement"]] forKey:@"announcement"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"avatar"]] forKey:@"imgpath"];
            
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"isrebate"]] forKey:@"isrebate"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"mdesc"]] forKey:@"mdesc"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"name"]] forKey:@"name"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"opentime"]] forKey:@"opentime"];
            
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"rebaterate"]] forKey:@"rebaterate"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"remark"]] forKey:@"remark"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"sellprice"]] forKey:@"sellprice"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"sellnum"]] forKey:@"sellnum"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"telephone"]] forKey:@"telephone"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"data"][@"totalIncome"]] forKey:@"totalIncome"];
            
            [userdefault setObject:@"Login_Success" forKey:@"Login_Success"];
            //保存用户名和密码
            [userdefault setObject:self.text_account.text forKey:@"account"];
            [userdefault setObject:self.text_password.text forKey:@"password"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Login_Success" object:nil];
            
            
            
            [SVProgressHUD showSuccessWithStatus:@"登录成功" maskType:SVProgressHUDMaskTypeBlack];
            
        }
        @catch (NSException *exception) {

        }
        @finally {
            
//            [self.navigationController popViewControllerAnimated:YES];

        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }   
}

#pragma mark - 找回密码点击事件
- (void)btn_foundPasswordAction:(UIButton *)sender
{
//    NSLog(@"找回密码");
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"拨打客服修改密码，点击确定拨打" preferredStyle:(UIAlertControllerStyleAlert)];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL * url1 = [NSURL URLWithString:@"tel://18660956655"];
        
        [[UIApplication sharedApplication] openURL:url1];
    }];
    
    [alert addAction:action1];
    
//    FoundPasswordViewController * foundPasswordViewController = [[FoundPasswordViewController alloc] init];
//    
//    [self showViewController:foundPasswordViewController sender:nil];
}

#pragma mark - textField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.text_account resignFirstResponder];
    [self.text_password resignFirstResponder];
}












@end
